package classification_service

import (
	"myserver/app/model"
	"myserver/app/types"
	"myserver/pkg/utils"
	"sort"
	"strings"
	"time"
)

type ClassificationService struct {
	Data *types.TableClassification
}

func (this_service *ClassificationService) Ceate() error {
	this_service.Data.Uuid = utils.GetUuid()
	if this_service.Data.ParentUuid != "" {
		uuids := make([]string, 0)
		uuids = append(uuids, this_service.Data.ParentUuid)
		datas, err := model.FindClassificationByUuid(uuids)
		if err != nil {
			return err
		}
		for i := 0; i < len(datas); i++ {
			s := strings.Split(datas[i].ChildUuids, ",")
			s = append(s, this_service.Data.Uuid)
			s1 := utils.FilterEmptyStrings(s)
			datas[i].ChildUuids = strings.Join(s1, ",")
		}
		model.UpDataClassification(&datas)

	}
	return model.AddClassification(this_service.Data)
}

func (ClassificationService) UpData(datas []types.TableClassification) error {
	var (
		uuids []string
	)
	for i := 0; i < len(datas); i++ {
		uuids = append(uuids, datas[i].Uuid)
	}
	ls, err := model.FindClassificationByUuid(uuids)
	if err != nil {
		return err
	}
	for i := 0; i < len(ls); i++ {
		ls1 := ls[i]
		for j := 0; j < len(datas); j++ {
			data2 := datas[j]
			if ls1.Uuid == data2.Uuid {
				ls[i].Name = datas[j].Name
				ls[i].ParentUuid = datas[j].ParentUuid
				ls[i].Shop = datas[j].Shop
				ls[i].Picture = datas[j].Picture
				ls[i].Notes = datas[j].Notes
				ls[i].Order = datas[j].Order
				ls[i].ChildUuids = datas[j].ChildUuids
			}

		}

	}
	return model.UpDataClassification(&ls)
}

func (ClassificationService) Dell(str string) error {
	var (
		uuids []string
	)
	uuids = strings.Split(str, ",")
	ls, err := model.FindClassificationByUuid(uuids)
	if err != nil {
		return err
	}
	for i := 0; i < len(ls); i++ {
		ls[i].IsDel = 1
		ls[i].DeleTime = time.Now()
	}
	return model.UpDataClassification(&ls)
}

func (ClassificationService) TreeList(shopUuid string) ([]types.SysClassification, error) {
	var (
		ls = make([]types.SysClassification, 0)
	)
	datas, _, err := model.GETList[types.TableClassification](-1, 0, "shop", shopUuid)
	if err != nil {
		return ls, err
	}
	ls, err = getTreeList(datas)
	if err != nil {
		return ls, err
	}
	return ls, nil
}
func getTreeList(datas []types.TableClassification) (types.SysClassificationByArr, error) {
	var (
		ls types.SysClassificationByArr
	)
	for i := 0; i < len(datas); i++ {
		data := datas[i]
		d1 := types.SysClassification{}
		d1.Clone(&data)
		if len(d1.ChildUuids) != 0 {
			childsDatas, err := fidechild(d1.ChildUuids, datas)
			if err != nil {
				return ls, err
			}
			d1.Childs = append(d1.Childs, childsDatas...)
		}
		if data.ParentUuid == "" {
			ls = append(ls, d1)
		}
	}
	sort.Sort(ls)
	return ls, nil
}
func fidechild(uuids []string, datas []types.TableClassification) (types.SysClassificationByArr, error) {
	var (
		ls types.SysClassificationByArr
	)
	for i := 0; i < len(uuids); i++ {
		uuid := uuids[i]
		for j := 0; j < len(datas); j++ {
			data := datas[j]
			if data.Uuid == uuid {
				d1 := types.SysClassification{}
				d1.Clone(&data)
				if len(d1.ChildUuids) != 0 {
					childsDatas, err := fidechild(d1.ChildUuids, datas)
					if err != nil {
						return ls, err
					}
					d1.Childs = append(d1.Childs, childsDatas...)
				}
				ls = append(ls, d1)
			}
		}

	}
	sort.Sort(ls)
	return ls, nil
}
